package com.onefish.toolkit.netty.heartbeat;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;

public class MyNettyServerHandler extends SimpleChannelInboundHandler<Object> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
        System.out.println("【服务端】 接收到的客户端-" + ctx.channel().remoteAddress() + " 发来的消息");
    }

    //如果IdleStateHandler检测到了超时事件，则会触发userEventTriggered方法
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = null;
            //获取超时事件：READER_IDLE、WRITER_IDLE或ALL_IDLE
            switch (event.state()) {
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
                default:
                    eventType = "未识别的超时事件";
                    break;
            }
            System.out.println("【服务端】 与客户端-"+ctx.channel().remoteAddress() + " 存在超时事件：" + eventType);
            //关闭超时事件的连接
//            ctx.channel().close();
        }
    }

}

